class Solution {
public:
    unordered_map<int, unordered_map<int, unordered_map<int, pair<int, int>>>> dp, flag;
    int maxOperations(vector<int>& nums) {
        int l = 0, r = nums.size() - 1;
        int n1 = nums[l] + nums[l + 1];
        int n2 = nums[r - 1] + nums[r];
        int n3 = nums[l] + nums[r];
        if (n1 == n2)
        {
            return max(dfs(l + 2, r, n1, nums), dfs(l + 1, r - 1, n3, nums)) + 1;
        }
        else return max(dfs(l, r - 2, n2, nums), max(dfs(l + 2, r, n1, nums), dfs(l + 1, r - 1, n3, nums))) + 1;
    }
    int dfs(int l, int r, int num, vector<int>& nums)
    {
        int ans = 0;
        if (dp[l][r][num].second) return dp[l][r][num].first;
        if (r - l + 1 < 2) return 0;
        int n1 = nums[l] + nums[l + 1];
        int n2 = nums[r - 1] + nums[r];
        int n3 = nums[l] + nums[r];
        if (n1 == num)
        {
            ans = max(ans, dfs(l + 2, r, num, nums) + 1);
        }
        else if (n2 == num)
        {
            ans = max(ans, dfs(l, r - 2, num, nums) + 1);
        }
        if (n3 == num)
        {
            ans = max(ans, dfs(l + 1, r - 1, num, nums) + 1);
        }
        dp[l][r][num].second = 1;
        return dp[l][r][num].first = ans;
    }
};